import { ref, computed, type Ref } from "vue";
import { defineStore } from "pinia";

import {
  getRecommend,
  getCookbook,
  getCookbookInfo,
  getSearchHot,
  getSearchSuggests,
  getSearch,
  getUser,
  getUserCookbook,
  getUserFavorites,
  getUserNote,
  getUserCalendar,
  getHomeNote,
  getNote,
  getNoteDetail,
  getClassification,
} from "@/api/index";

export const useDouguoStore = defineStore("douguo", () => {
  // 首页推荐
  const recommend = ref({
    banner: [],
    list: [],
  });
  async function updataRecommend() {
    const res = await getRecommend(0);
    recommend.value = res.data.result;
    // console.log(res.data.result);
  }

  // 菜谱详情
  const cookbook = ref({
    recipe: {
      as: 0,
      title: "",
      views_count_text: "",
      favo_counts_text: "",
      user: {
        user_id: "",
        user_photo: "",
        nickname: "",
        lvl: "",
      },
      cookstorys: [],
      cookstory: "",
      tips: "",
      photo_path: "",
      cook_time: "",
      cook_time_image: "",
      cook_difficulty: "",
      cook_difficulty_image: "",
      major: [],
      nutrition_facts: [
        {
          calorie: 0,
        },
      ],
      cookbook: "",
      nutrition_facts_hint_text: "",
      nutrition_facts_hint_text_url: "",
      cookstep: [],
      release_time: "",
      comments_count: "",
      dish_count_text: "",
      cook_id: "",
      thumb_path: "",
    },
  });
  async function updataCookbook(id: number) {
    const res = await getCookbook(id);
    cookbook.value = res.data.result;
    // console.log(res.data.result);
  }

  // 菜谱相关详情
  const cookbookInfo = ref({
    first_level_comment_count: "",
    comments: [],
  });
  async function updataCookbookInfo(id: number) {
    const res = await getCookbookInfo(id);
    cookbookInfo.value = res.data.result;
    // console.log(res.data.result);
  }

  // 收藏管理选择
  const collect = ref([]);
  function updataCollect() {
    collect.value = JSON.parse(localStorage.getItem("collect") || "[]");
    // const arr = JSON.parse(localStorage.getItem("collect") || "[]").reverse();
    // collect.value = arr;
  }
  const choosed = ref([]);
  function updataChoosed(id: number) {
    if (id) {
      if (choosed.value.indexOf(id) == -1) {
        choosed.value.push(id);
      } else {
        choosed.value = choosed.value.filter((o) => o != id);
      }
    }
  }
  function cancelCollect() {
    choosed.value = [];
    // const arr = JSON.parse(localStorage.getItem("collect") || "[]").reverse();
    collect.value = JSON.parse(localStorage.getItem("collect") || "[]");
  }

  // 热搜
  const searchHot = ref({
    suggestdetails: [],
  });
  async function updataSearchHot() {
    const res = await getSearchHot();
    searchHot.value = res.data.result;
    // console.log(res.data.result);
  }

  // 搜索建议
  const searchSuggests = ref({
    suggests: [],
    tag: {},
  });
  async function updataSearchSuggests(kw: string) {
    const res = await getSearchSuggests(kw);
    searchSuggests.value = res.data.result;
    // console.log(res.data.result);
  }

  // 搜索结果
  const search = ref({
    list: [],
  });
  async function updataSearch(obj: object) {
    // console.log(obj);

    const res = await getSearch(obj);
    search.value = res.data.result;
    // console.log(res.data.result);
  }

  // 用户详情
  const user = ref({
    user: {
      userid: "",
      nick: "",
      lvl: "",
      verified_image: "",
      avatar_medium: "",
      usermedal: {
        icon: "",
        count: "",
        count_text: "",
      },
      age: "",
      ip_address_location: "",
      location: "",
      introduction: "",
      following_count_text: "",
      followers_count_text: "",
      favoed_count_text: "",
      noted_count_text: "",
      point_text: "",
      recipes_count: "",
      notes_count: "",
      collect_count: "",
    },
    relationship: 0,
  });
  async function updataUser(id: string) {
    const res = await getUser(id);
    user.value = res.data.result;
    // console.log(res.data.result);
  }

  // 用户发布菜谱
  const userCookbook = ref({
    list: [
      {
        r: {
          id: "",
          img: "",
          recommend_label: "",
          n: "",
        },
        type: 0,
      },
    ],
  });
  async function updataUserCookbook(obj: object) {
    const res = await getUserCookbook(obj);
    userCookbook.value = res.data.result;
    // console.log(res.data.result);
  }

  // 用户收藏
  const userFavorites = ref({
    recipes: [
      {
        id: "",
        img: "",
        recommend_label: "",
        a: {
          n: "",
          p: "",
        },
      },
    ],
  });
  async function updataUserFavorites(obj: object) {
    const res = await getUserFavorites(obj);
    userFavorites.value = res.data.result;
    // console.log(res.data.result);
  }

  // 用户发布的笔记
  const userNote = ref({
    notes: [
      {
        month_text: "",
        day_text: "",
        day_list: [
          {
            images: [
              {
                u: "",
              },
            ],
            title: "",
          },
        ],
      },
    ],
    straight_days: "",
  });
  async function updataUserNote(obj: object) {
    const res = await getUserNote(obj);
    userNote.value = res.data.result;
    // console.log(res.data.result);
  }

  // 用户日历
  const userCalendar = ref({});
  async function updataUserCalendar(obj: object) {
    const res = await getUserCalendar(obj);
    userCalendar.value = res.data.result;
    // console.log(res.data.result);
  }

  // 首页笔记
  const homeNote = ref({
    list: [
      {
        type: 0,
        activity: {
          image: "",
          name: "",
          author: {
            n: "",
            p: "",
          },
          action_title: "",
          action_icon: "",
          action_url: "",
        },
        note: {
          image_u: "",
          title: "",
          author: {
            n: "",
            p: "",
            id: 0,
          },
          favo_count: "",
          like_count: "",
          id: 0,
        },
      },
    ],
    topics: [
      [
        {
          id: "",
          name: "",
          icon: "",
        },
      ],
    ],
  });
  async function updataHomeNote() {
    const res = await getHomeNote(0);
    homeNote.value = res.data.result;
    // console.log(res.data.result);
  }

  // 笔记详情
  const note = ref({
    author: {
      p: "",
      lvl: "",
      n: "",
      id: "",
    },
    straight_text: "",
    images: [
      {
        u: "",
      },
    ],
    contents: [
      {
        type: -1,
        icon: "",
        color: "",
        c: "",
        id: "",
      },
    ],
    selection_text: "",
    like_count_text: "",
  });
  async function updataNote(id: number) {
    const res = await getNote(id);
    note.value = res.data.result.note;
    // console.log(res.data.result);
  }

  // 笔记相关详情
  const noteDetail = ref({});
  async function updataNoteDetail(id: number) {
    const res = await getNoteDetail(id);
    noteDetail.value = res.data.result;
    // console.log(res.data.result);
  }

  // 菜谱分类
  const classification = ref({
    cs: [
      {
        id: "",
        name: "",
        cs: [
          {
            id: "",
            name: "",
            image_url: "",
            cs: [
              {
                id: "",
                name: "",
              },
            ],
          },
        ],
      },
    ],
  });
  async function updataClassification() {
    const res = await getClassification();
    classification.value = res.data.result;
    // console.log(res.data.result);
  }

  return {
    recommend,
    updataRecommend,
    cookbook,
    updataCookbook,
    cookbookInfo,
    updataCookbookInfo,
    collect,
    updataCollect,
    choosed,
    updataChoosed,
    cancelCollect,
    searchHot,
    updataSearchHot,
    searchSuggests,
    updataSearchSuggests,
    search,
    updataSearch,
    user,
    updataUser,
    userCookbook,
    updataUserCookbook,
    userFavorites,
    updataUserFavorites,
    userNote,
    updataUserNote,
    userCalendar,
    updataUserCalendar,
    homeNote,
    updataHomeNote,
    note,
    updataNote,
    noteDetail,
    updataNoteDetail,
    classification,
    updataClassification,
  };
});
